<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.css" rel="stylesheet" />
		<link href="../css/contact.css" rel="stylesheet" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" id="return"></a>
			<h1 class="mui-title">通讯录</h1>
		</header>
		<div class="mui-content">
			<!-- 手机通讯录列表 mlist -->
			<ul class="mui-table-view" id="contacts"></ul>
		</div>
		<script src="../js/mui.js"></script>
		<script type="text/javascript">
			mui.init()
			// 通讯录 https://www.html5plus.org/doc/zh_cn/contacts.html
			mui.plusReady(function (){
				/**
				 * type: ( Number ) 必选 要获取的通讯录类型:手机通讯录(ADDRESSBOOK_PHONE)或SIM卡通讯录(ADDRESSBOOK_SIM)
				 * successCB: ( AddressBookSuccessCallback ) 必选 获取通讯录对象成功回调
				 * errorCB: ( ContactsErrorCallback ) 可选 获取通讯录对象失败回调
				 */
				plus.contacts.getAddressBook(
					plus.contacts.ADDRESSBOOK_PHONE,
					function(addressbook) {
						// 将回调结果处理为数组
						addressbook.find([], function(res) {
							if(res.length == 0) return;
							// console.log(JSON.stringify(res));
							var li_str = "";
							for (var i = 0; i < res.length; i++) {
								if(res[i].phoneNumbers.length == 0) {
									continue;
								}
								li_str += '<li class="mui-table-view-cell" onclick="dial(\''+res[i].phoneNumbers[0].value+'\')">'
											   +'<a class="mui-navigate-right">'
											       +'<span>'+res[i].displayName+'</span>'
											       +'<span>'+res[i].phoneNumbers[0].value+'</span>'
											   +'</a>'
										+'</li>';
							}
							document.getElementById("contacts").innerHTML = li_str;
						});
					},
					function(e) {
						alert("读取通讯录失败!" + e);
					}
				)
			});
			
			// 拨打电话函数
			function dial (phoneNumber) {
				// 去空格
				var telNum = phoneNumber.replace(/\s*/g, "");
				console.log("拨打电话："+telNum);
				// https://www.html5plus.org/doc/zh_cn/device.html#plus.device.dial
				plus.device.dial(telNum);
			}
		</script>
	</body>

</html>